package com.dm.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dm.entity.CompanyInfoDO;
import com.dm.entity.PerationLogDo;
import com.dm.entity.SysUserDO;
import com.dm.query.QueryCompanyInfo;
import com.dm.service.PerationLogService;
import com.dm.utils.RespsonseResult;


@Controller
@RequestMapping("log")
public class PerationLogController {
	
	@Autowired
	PerationLogService serationLogService ;
	
	private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	
	@RequestMapping("selectlog")
	public @ResponseBody Object selectLog(HttpServletRequest request,PerationLogDo perationLogDo){
		 logger.info("日志查询");
		 RespsonseResult result = new RespsonseResult();
		 try {
			 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
			 if(userInfo.getType() !=0 ){
				 perationLogDo.setPerationUserId(userInfo.getUserId()+"");
			 }
			 List<PerationLogDo> data  = serationLogService.selectPerationLogList(perationLogDo);
		     DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
			 for(PerationLogDo index : data){
				 String tsStr = sdf.format(index.getPerationTime());  
		         index.setPerationTimeStr(tsStr); 
			 }
			 result.setData(data);
			 result.setMsg("查询成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("查询出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	@RequestMapping("dellog")
	public @ResponseBody Object dellog(HttpServletRequest request,PerationLogDo perationLogDo){
		 logger.info("日志删除");
		 RespsonseResult result = new RespsonseResult();
		 try {
			 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
			 if(userInfo.getType() !=0 ){
				 result.setMsg("权限不足");
				 return JSON.toJSON(result);
			 }
			 if(perationLogDo.getPerationId() == null){
				 result.setMsg("参数不能为空");
				 return JSON.toJSON(result);
			 }
			 serationLogService.dellog(perationLogDo);
			 result.setMsg("删除成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("删除出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}

}
